package com.jsb.common.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.LoggerFactory;

import com.jsb.common.config.Global;

public class PicUtil extends Exceptions{
	private final static org.slf4j.Logger log = LoggerFactory.getLogger(PicUtil.class);
	 public static void getPhoto(String url,String path, HttpServletResponse response) {
         response.setHeader("Content-type", "text/html;charset=UTF-8");
         try {
             File file = new File(path +"/"+ url);
             log.error("获取图片真实路径为："+path+"/"+url);
             InputStream inStream = null;
             inStream = new FileInputStream(file);
             byte data[] = readInputStream(inStream);
             if(url.contains(".")){
            	 response.setContentType("image/" + url.substring(url.lastIndexOf(".")+1)); // 设置返回的文件类型
        	 }else{
        		 response.setContentType("image/png"); // 设置返回的文件类型
        	 }
             OutputStream os = response.getOutputStream();
             os.write(data);
             os.flush();
             os.close();
         } catch (FileNotFoundException e) {
        	 log.error(getErrorMsg(e)+"文件名："+url);
         } catch (IOException e) {
        	 log.error(getErrorMsg(e)+"获取图片"+url);
         }

     }


     public static byte[] readInputStream(InputStream inStream) {
         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
         byte[] buffer = new byte[2048];
         int len = 0;
         try {
             while ((len = inStream.read(buffer)) != -1) {
                 outStream.write(buffer, 0, len);
             }
             inStream.close();
         } catch (IOException e) {
             e.printStackTrace();
         }

         return outStream.toByteArray();
     }
     public static String getPicUploadPath(){
         String path = "";
         String osName = System.getProperties().getProperty("os.name").toLowerCase();
         if(osName.contains("windows")){
             path =  Global.getConfig("windows.pic");
         }else if(osName.contains("linux")){
             path =  Global.getConfig("linux.pic");
         }
         log.warn("当前操作系统为："+osName+",当前文件路径："+path);
         return path;
     }
     /**
      * 获取问题中的图片
      * @param s
      * @return  图片名称的集合
      */
     public static List<String> getQuestionPicName(String s){
    	 if(StringUtils.isEmpty(s)){
    		 return null;
    	 }
    	 List<String> strlist = new ArrayList<String>();
    	 String str = s.substring(1, s.length()-1);
    	 str = str.replace("\"", "");
    	 if(str.indexOf(",")>0){
    		 String[] strarr = str.split(",");
    		 for (String ss : strarr) {
    			 strlist.add(ss);
			}
    	 }else{
    		 strlist.add(str);
    	 }
    	 return strlist;
     }
     
     public static void main(String[] args) {
//		String str = "[\"13a83223d79f486a99458d790d9d362e1\",\"13a83223d79f486a99458d790d9d362e2\",\"13a83223d79f486a99458d790d9d362e3\"]";
		String str = "[\"13a83223d79f486a99458d790d9d362e1\"]";
		List<String> questionPicName = getQuestionPicName(str);
		for (String string : questionPicName) {
			System.out.println(string);
		}
	}
}
